<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<title></title>
<jsp:include page="../links.jsp"></jsp:include>
</head>
<body>
	<script type="text/javascript">
		var news_datagrid;
		var news_searchForm;
		var news_addDlg;
		var news_addForm;
		var news_updateDlg;
		var news_updateForm;
		$(function() {
			news_datagrid = $('#news_datagrid').datagrid({
				url : getRootPath() + '/admin/newsAction/datagrid.do',
				title : '新闻列表',
				iconCls : 'icon-news',
				pagination : true,
				rownumbers : true,
				selectOnCheck : true,
				checkOnSelect : false,
				pageSize : 10,
				pageList : [ 10, 20, 30, 40, 50 ],
				fit : true,
				fitColumns : true,
				nowarp : false,
				border : false,
				idField : 'nid',
				sortOrder : 'asc',
				columns : [ [ {
					title : '',
					field : 'id',
					width : 100,
					checkbox : true
				}, {
					title : '文章编号',
					field : 'nid',
					width : 50,
				}, {
					title : '标题',
					field : 'title',
					width : 200,
				}, {
					title : '发布时间',
					field : 'pubdate',
					width : 100,
				}, {
					title : '内容',
					field : 'content',
					width : 200,
					formatter : function(content, row, index) {
						if (content.length >= 25) {
							return (content.substr(0, 25) + "...");
						} else {
							return content;
						}
					}
				}, {
					title : '点击次数',
					field : 'hitcount',
					width : 80
				}, {
					title : '操作',
					field : '',
					width : 100,
				} ] ],
				toolbar : [ {
					text : '添加',
					iconCls : 'icon-add',
					handler : function() {
						showAddDlg();
					}
				}, '-', {
					text : '修改',
					iconCls : 'icon-edit',
					handler : function() {
						showUpdateDlg();
					}
				}, '-', {
					text : '删除',
					iconCls : 'icon-remove',
					handler : function() {
						remove();
					}
				}, '-', {
					text : '查看',
					iconCls : 'icon-ok',
					handler : function() {
						showUpdateDlg();
					}
				} ]
			});//datagrid end
			//过滤查询
			news_searchForm = $('#news_searchForm').form();
			doSearch = function() {
				//console.info(serializeObject(news_searchForm));
				news_datagrid.datagrid('load', serializeObject(news_searchForm));
			};
			//重置
			cleanSearch = function() {
				news_datagrid.datagrid('load', {});
				news_searchForm.find('input').val('');
			};
			//新增
			news_addDlg = $('#news_addDlg').dialog();
			news_addForm = $('#news_addForm').form();
			showAddDlg = function() {
				$('#news_addForm input').val('');
				news_addDlg.dialog('open')
			};
			doAdd = function() {
				news_addForm.form('submit', {
					url : '${pageContext.request.contextPath}/admin/newsAction/add.do?',
					success : function(data) {
						var r = $.parseJSON(data);
						if (r && r.success) {
							news_addDlg.dialog('close');
							cleanSearch();
							$.messager.show({
								title : '提示',
								msg : r.msg,
								showType : 'show'
							});
						} else {
							$.messager.alert('提示', r.msg);
						}
					}
				});
			};
			//修改
			news_updateDlg = $('#news_updateDlg').dialog();
			news_updateForm = $('#news_updateForm').form();
			showUpdateDlg = function() {
				var selectids = news_datagrid.datagrid('getChecked');
				if (selectids.length == 1) {
					news_updateDlg.dialog('open');
					$('#news_up_nid').val(selectids[0].nid);
					$('#news_up_title').val(selectids[0].title);
					$('#news_up_pubdate').val(selectids[0].pubdate);
					$('#news_up_content').val(selectids[0].content);
				} else {
					$.messager.alert('提示', "请勾选一项");
				}
			};
			doUpdate = function() {
				news_updateForm.form('submit', {
					url : '${pageContext.request.contextPath}/admin/newsAction/update.do?',
					success : function(data) {
						var r = $.parseJSON(data);
						if (r && r.success) {
							news_updateDlg.dialog('close');
							cleanSearch();
							$.messager.show({
								title : '提示',
								msg : r.msg,
								showType : 'show'
							});
						} else {
							$.messager.alert('提示', r.msg);
						}
					}
				});
			};
			//删除
			remove = function() {
				var selectids = news_datagrid.datagrid('getChecked');
				var ids = [];
				if (selectids.length > 0) {
					$.messager.confirm('确认', '您是否要删除当前选中的项目？', function(r) {
						if (r) {
							for (var i = 0; i < selectids.length; i++) {
								ids.push(selectids[i].uid);
							}
							$.ajax({
								url : '${pageContext.request.contextPath}/admin/newsAction/delete.do',
								data : {
									ids : ids.join(',')
								},
								dataType : 'json',
								success : function(r) {
									news_datagrid.datagrid('load');
									news_datagrid.datagrid('unselectAll');
									$.messager.show({
										title : '提示',
										msg : r.msg
									});
								}
							});
						}
					});
				} else {
					$.messager.alert('提示', "请最少勾选一项");
				}
			};
		});
	</script>
	<div class="easyui-layout" data-options="fit:true,border:false">
		<div data-options="region:'north',title:'模糊查询条件：(右边可折叠)'" style="height: 100%;">
			<form id="news_searchForm" method="post">
				<table style="width: 100%; height: 60%;">
					<tr>
						<th class="th-search-label">文章标题:</th>
						<td><input name="news.title" /></td>
						<th class="th-search-label">文章内容:</th>
						<td><input name="news.content" /></td>
					</tr>
					<tr>
						<th class="th-search-label">发布时间</th>
						<td><input name="news.pubdateStart" class="easyui-datetimebox" editable="false" /></td>
						<th class="th-search-label">至</th>
						<td><input name="news.pubdateEnd" class="easyui-datetimebox" editable="false" />&nbsp;<a
							class="easyui-linkbutton" style="width: 25%" data-options="iconCls:'icon-search'"
							href="javascript:void(0)" onclick="doSearch();">查询</a>&nbsp;<a class="easyui-linkbutton"
							style="width: 25%" data-options="iconCls:'icon-redo'" href="javascript:void(0)"
							onclick="cleanSearch();">清空</a></td>
					</tr>
				</table>
			</form>
		</div>
		<div data-options="region:'center',title:''," style="overflow: hidden;">
			<table id="news_datagrid">
			</table>
		</div>
		<div id="news_addDlg"
			data-options="closed:true,modal:true,title:'添加用户',buttons:[
				{text : '添加',iconCls : 'icon-add',handler : function() {doAdd();}},
				{text : '取消',iconCls : 'icon-cancel',handler : function() {news_addDlg.dialog('close');}}]"
			class="easyui-dialog" style="width: 680px; height: 350px;" align="center">
			<form id="news_addForm" method="post">
				<table>
					<tr>
						<th>文章标题:</th>
						<td><input name="news.title" type="text" class="easyui-validatebox" data-options="required:true" /></td>
						<th>发布日期:</th>
						<td><input name="news.pubdate" class="easyui-validatebox easyui-datetimebox" editable="false"
							type="text" data-options="required:true" /></td>
					</tr>
					<tr>
						<th>文章内容:</th>
						<td colspan="3"><textarea name="news.content" class="easyui-validatebox"
								data-options="required:true" style="height: 200px; width: 560px;"></textarea></td>
				</table>
			</form>
		</div>
		<div id="news_updateDlg"
			data-options="closed:true,modal:true,title:'修改用户',buttons:[
				{text : '修改',iconCls : 'icon-save',handler : function() {doUpdate();}},
				{text : '取消',iconCls : 'icon-cancel',handler : function() {news_updateDlg.dialog('close');}}]"
			class="easyui-dialog" style="width: 700px; height: 350px;" align="center">
			<form id="news_updateForm" method="post">
				<table>
					<tr>
						<th>编号:</th>
						<td><input id="news_up_nid" name="news.nid" maxLength=10 class="easyui-validatebox" type="number"
							data-options="required:true,validType:['length[0,10]','number']" readonly="readonly" /></td>
						<th>文章标题:</th>
						<td><input id="news_up_title" name="news.title" type="text" class="easyui-validatebox"
							data-options="required:true" /></td>
					</tr>
					<tr>
						<th>发布日期:</th>
						<td><input id="news_up_pubdate" name="news.pubdate" class="easyui-validatebox easyui-datetimebox"
							editable="false" type="text" data-options="required:true" /></td>
						<th>点击次数:</th>
						<td><input id="news_up_hitcount" name="news.hitcount" maxLength=10 class="easyui-validatebox"
							type="number" data-options="validType:['length[0,10]','number']" /></td>
					</tr>
					<tr>
						<th>文章内容:</th>
						<td colspan="3"><textarea id="news_up_content" name="news.content" class="easyui-validatebox"
								data-options="required:true" style="height: 200px; width: 560px;"></textarea></td>
					<tr>
					<tr>
				</table>
			</form>
		</div>
		<div id="news_showDlg"></div>
	</div>
</body>
</html>